<template>
  <view>
    <view class="search-box"><my-search bgcolor="#56dcd8"></my-search></view>
    <!-- 轮播图区域 -->
    <swiper
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="1000"
    >
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator
          :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"
          class="swiper-item"
        >
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="navClick(item)"
      >
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层 -->
    <view class="floor-list">
      <!-- 每一层楼： -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 标题区域： -->
        <image
          :src="item.floor_title.image_src"
          mode=""
          class="title-img"
        ></image>
        <!-- 图片区域： -->
        <view class="floor-img-box">
          <!-- 左侧图片的盒子： -->
          <navigator class="left-box" :url="item.product_list[0].new_url">
            <!-- 左侧图片直接取数组的第一项： -->
            <image
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              :src="item.product_list[0].image_src"
              mode="widthFix"
            ></image>
          </navigator>
          <!-- 右侧图片的盒子： -->
          <view class="right-box">
            <navigator
              v-for="(item1, index1) in item.product_list"
              :key="index1"
              :url="item1.new_url"
              v-if="index1 !== 0"
            >
              <image
                :src="item1.image_src"
                mode="widthFix"
                :style="{ width: item1.image_width + 'rpx' }"
              ></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import badgeMix from '@/mixins/tabbar-badge.js';
export default {
  mixins: [badgeMix],
  data() {
    return {
      // 轮播图数组
      swiperList: [],
      // 分类导航的数组
      navList: [],
      // 楼层数组
      floorList: []
    };
  },
  onLoad() {
    // 页面刚加载的时候 轮播图
    this.getSwiperList();
    // 分类导航
    this.getNavList();
    // 楼层
    this.getFloorList();
  },
  methods: {
    // 点击跳转到分类页面
    navClick(item) {
      if (item.name === '分类') {
        uni.switchTab({
          url: '/pages/cate/cate'
        });
      }
    },
    // 请求轮播图数据的方法
    async getSwiperList() {
      const { data } = await uni.$http.get('/api/public/v1/home/swiperdata');
      // console.log(data);
      if (data.meta.status !== 200) {
        return uni.$showMsg();
      }
      this.swiperList = data.message;
    },
    // 请求分类导航
    async getNavList() {
      const { data } = await uni.$http.get('/api/public/v1/home/catitems');
      console.log(data);
      if (data.meta.status !== 200) {
        return uni.$showMsg();
      }
      this.navList = data.message;
    },
    // 请求楼层数据
    async getFloorList() {
      const { data } = await uni.$http.get('/api/public/v1/home/floordata');
      console.log(data);
      if (data.meta.status !== 200) {
        return uni.$showMsg();
      }
      // 处理url字段
      data.message.forEach(floor => {
        floor.product_list.forEach(item => {
          item.new_url =
            '/subpkg/goods_list/goods_list?' + item.navigator_url.split('?')[1];
        });
      });
      this.floorList = data.message;
    }
  }
};
</script>

<style lang="scss">
swiper {
  height: 330rpx;
}
.swiper-item,
image {
  width: 100%;
  height: 100%;
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;
  .nav-item {
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }
}
.floor-item {
  .title-img {
    height: 60rpx;
  }
  .floor-img-box {
    display: flex;
    .right-box {
      display: flex;
      flex-wrap: wrap;
      image {
        padding-left: 10rpx;
      }
    }
  }
}
.search-box {
  position: sticky;
  top: 0;
  z-index: 99;
}
</style>
